package com.gxa.community.admin.controller.content;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.community.admin.advice.Log;
import com.gxa.community.admin.service.content.QueryTopicService;
import com.gxa.community.admin.service.content.TopicService;
import com.gxa.community.core.pojo.bo.content.TopicBO;
import com.gxa.community.core.pojo.dto.content.PageTopicDTO;
import com.gxa.community.core.pojo.dto.content.TopicGoodsDto;
import com.gxa.community.core.pojo.vo.content.TopicVo;
import com.gxa.community.core.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/content")
public class TopicController {
    @Autowired
    private TopicService topicService;
    @Autowired
    private QueryTopicService queryTopicService;

    @ApiOperation("根据id查询单个专题信息")
    @GetMapping("/getsingletopicbyid/{id}")
    public R getSingleTopicById(@PathVariable("id") Integer id){

        return topicService.getSingleTopicById(id);
    }

    @ApiOperation("查询所有专题")
    @GetMapping("/getalltopic")
    public R getAllTopic(@RequestParam("currentPage") Integer currentPage,@RequestParam("pageSize") Integer pageSize){
        //设置分页条件
        PageHelper.startPage(currentPage,pageSize);
        List<TopicBO> list = queryTopicService.list();
        if (!CollectionUtils.isEmpty(list)){
            //封装响应数据
            PageTopicDTO pageTopicDTO = new PageTopicDTO();
            PageInfo<TopicBO> pageInfo = new PageInfo<>(list);
            pageTopicDTO.setTotal(pageInfo.getTotal());
            pageTopicDTO.setPages(pageInfo.getPages());
            pageTopicDTO.setCurrentPage(currentPage);
            pageTopicDTO.setPageSize(pageSize);
            pageTopicDTO.setTopics(pageInfo.getList());
            return R.ok("查询成功！").put("data",pageTopicDTO);
        }else {
            return R.error(600,"查询失败，请联系管理员！！");
        }
    }

    @ApiOperation("按专题名称搜索专题")
    @GetMapping("/searchtopicbyname")
    public R searchTopicByName(@RequestParam(value = "name",required = false) String name,@RequestParam("currentPage") Integer currentPage,@RequestParam("pageSize") Integer pageSize){
        if (StringUtils.isBlank(name)){
            //不输入则默认查询所有
            return this.getAllTopic(currentPage, pageSize);
        }else {
            //封装查询条件，按name模糊查询
            LambdaQueryWrapper<TopicBO> topicBOLambdaQueryWrapper = Wrappers.lambdaQuery(TopicBO.class);
            topicBOLambdaQueryWrapper.like(TopicBO::getName,name);
            List<TopicBO> list = queryTopicService.list(topicBOLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(list)){
                //封装响应数据
                PageTopicDTO pageTopicDTO = new PageTopicDTO();
                PageInfo<TopicBO> pageInfo = new PageInfo<>(list);
                pageTopicDTO.setTotal(pageInfo.getTotal());
                pageTopicDTO.setPages(pageInfo.getPages());
                pageTopicDTO.setCurrentPage(currentPage);
                pageTopicDTO.setPageSize(pageSize);
                pageTopicDTO.setTopics(pageInfo.getList());
                return R.ok("查询成功！").put("data",pageTopicDTO);
            }else {
                return R.error(600,"查询失败，请联系管理员！！");
            }
        }
    }

    @ApiOperation("新建专题")
    @PostMapping("/addtopic")
    @Log(title = "",action = "新增了专题")
    public R addTopic(@RequestBody TopicVo topicVo){
        return topicService.addTopic(topicVo);
    }

    @ApiOperation("修改单个专题")
    @PutMapping("/updatetopicbyid")
    @Log(title = "",action = "修改了专题")
    public R updateTopicById(@RequestBody TopicVo topicVo){
//        log.info("进入该方法了==========》");
        return topicService.updateTopicById(topicVo);
    }

    @ApiOperation("根据id删除专题")
    @DeleteMapping("/deletebyid/{id}")
    @Log(title = "",action = "删除了专题")
    public R delTopicById(@PathVariable("id") Integer id){
        return topicService.delTopicById(id);
    }

    @ApiOperation("禁用或启用专题接口")
    @PutMapping("/updatetopicstatus/{id}")
    @Log(title = "",action = "禁用或启用专题接口")
    public R updateTopicStatus(@PathVariable("id") Integer id){

        return topicService.updateTopicStatus(id);
    }

    /**
     * 远程接口
     * 获取所有的专题信息
     * @return List<TopicGoodsDto>
     */
    @GetMapping("/queryTopList")
    public List<TopicGoodsDto> queryTopList(){
        return topicService.queryTopList();
    }
}
